package template.page.admin;

import java.util.List;

import org.apache.click.ActionListener;
import org.apache.click.Control;
import org.apache.click.control.AbstractLink;
import org.apache.click.control.ActionButton;
import org.apache.click.control.ActionLink;
import org.apache.click.control.Column;
import org.apache.click.control.PageLink;
import org.apache.click.control.Table;
import org.apache.click.dataprovider.DataProvider;
import org.apache.click.extras.control.LinkDecorator;

import template.page.BorderPage;

@SuppressWarnings("serial")
public class ManageGames extends BorderPage {

    public String title = "Manage Games";

	private Table gameTable = new Table("gameTable");
	private PageLink editGameLink = new PageLink("Edit",EditGame.class);
	private ActionButton createGameButton = new ActionButton("addGameButton","Add Game");
	private ActionLink deleteGameLink = new ActionLink("Delete",this,"onDeleteGameClick");	
	
	@SuppressWarnings("rawtypes")
	public ManageGames() 
	{
		addControl(gameTable);
		addControl(editGameLink);
		addControl(deleteGameLink);
		addControl(createGameButton);
		
		// Setup table
		gameTable.setClass(Table.CLASS_ITS);
		gameTable.setPageSize(50);
		gameTable.setShowBanner(true);
		gameTable.setSortable(true);

		gameTable.addColumn(new Column("id"));
		gameTable.addColumn(new Column("name"));
		gameTable.addColumn(new Column("minPlayer"));
		gameTable.addColumn(new Column("maxPlayer"));
		gameTable.addColumn(new Column("duration"));		
        
		editGameLink.setImageSrc("/assets/images/actionEdit.gif");
		editGameLink.setTitle("Edit game");
        
		createGameButton.setTitle("Create game");
		createGameButton.setListener(this, "onCreateGameClick");
        
		deleteGameLink.setImageSrc("/assets/images/actionDelete.gif");
		deleteGameLink.setTitle("Delete game");
		deleteGameLink.setAttribute("onclick",
            "return window.confirm('Are you sure you want to delete this record?');");

        Column column = new Column("Action");
        column.setTextAlign("center");
        AbstractLink[] links = new AbstractLink[] { editGameLink, deleteGameLink };
        column.setDecorator(new LinkDecorator(gameTable, links, "id"));
        column.setSortable(false);
        gameTable.addColumn(column);

        // Table rowList will be populated through a DataProvider which loads
        // data on demand.
        gameTable.setDataProvider(new DataProvider() {

            public List getData() {
                return getGameSessionService().getAllGames();
            }
        });

        // Below we setup the table to preserve it's state (sorting and paging)
        // while editing customers
        gameTable.getControlLink().setActionListener(new ActionListener() {

            public boolean onAction(Control source) {
                // Save Table sort and paging state between requests.
                // NOTE: we set the listener on the table's Link control which is invoked
                // when the Link is clicked, such as when paging or sorting.
                // This ensures the table state is only saved when the state changes, and
                // cuts down on unnecessary session replication in a cluster environment.
            	gameTable.saveState(getContext()); 
                return true;
            }
        });

        // Restore the table sort and paging state from the session between requests
        gameTable.restoreState(getContext()); 
	}
	
	/**
     * Handle the delete row click event.
     */    
    public boolean onDeleteGameClick() {
        Long id = deleteGameLink.getValueLong();
        getGameSessionService().deleteGame(id);
        return true;
    }
    
	/**
     * Handle the delete row click event.
     */    
    public boolean onCreateGameClick() {
    	setRedirect(EditGame.class);        
        return true;
    }

}
